/*
 * Creado el 26/02/2008
 *
 */
package ar.pmt.vista.acciones;


import java.util.Collection;
import java.util.Iterator;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import ar.pmt.modelo.beans.Peticion;
import ar.pmt.modelo.beans.Proyecto;
import ar.pmt.modelo.businessObject.SrvcControlAvance;

/**
 * @author guille
 *
 */
public class CargarPeticionesAjaxAction extends PMTAction {

    
 
	/**
	 * @param mapping ActionMapping
	 * @param form ActionForm
	 * @param request HttpServletRequest
	 * @param response HttpServletResponse
	 * @return ActionForward
	 * @throws Exception control general de errores de la aplicacion
	 */
    public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
	     	SrvcControlAvance srvcControlAvance= (SrvcControlAvance)beanFactory.getBean("srvcControlAvance");
	        
	        Proyecto proyectoSel= srvcControlAvance.obtenerProyecto(new Integer(request.getParameter("proyectoId")));
	    
	        Collection<Peticion> peticiones= proyectoSel.getPeticiones();
	        //Hibernate.initialize(peticiones);
	        
	        StringBuffer xml = new StringBuffer("<?xml version=\"1.0\" encoding=\"UTF-8\"?><regiones>");
	        if(proyectoSel != null) {
				for (Iterator<Peticion> iterator = peticiones.iterator(); iterator.hasNext();) {
					Peticion peticion = (Peticion) iterator.next();
				    xml.append("<peticion name='");
					xml.append(peticion.getNombre()); 
					xml.append("' ");
					xml.append("value='");
					xml.append(peticion.getId());
					xml.append("' />");
				}
				xml.append("</regiones>");
				response.addHeader("Pragma", "no-cache");
				response.addHeader("Cache-Control", "no-cache");
				response.setContentType("text/xml; charset=utf-8");
	            response.getWriter().print(xml.toString());
	      
	        }
        	
        return null;
	}
}
